rm(list=ls())
gc()

library(data.table)

# This script makes the count data using the Florida 2009 voterfile.

fl.reg = fread('~/Research_Group Dropbox/Jacob Brown/RCE_shared/voterfile_backup/voterfile_backup_csvs/fl_august09_registrants_wcolnames.csv',
               select = c('voterid', 'race', 'partyaffiliation', 'birthdate'))
fl.v = fread('~/Research_Group Dropbox/Jacob Brown/RCE_shared/voterfile_backup/voterfile_backup_csvs/fl_august09_history.csv', 
             select = c('voterid', 'electiondate'))
gc()

fl = merge(fl.v, fl.reg[,c("voterid", "race", "partyaffiliation", "birthdate")], all.x = T)
rm(fl.v)
gc()

fl.count = fl[year(as.Date(birthdate))>=1975 & race %in% 3:5 & 
                electiondate %in% as.Date(c("2008-11-04", "2006-11-07", "2004-11-02", "2002-11-05", "2000-11-07")),
              list(vote = .N  ), 
              by = c("birthdate", "race", "electiondate")]
rm(fl)
gc()

fl.reg$dem = ifelse(fl.reg$partyaffiliation == "DEM", 1, 0)
fl.reg$rep = ifelse(fl.reg$partyaffiliation == "REP", 1, 0)
fl.reg$majorparty = ifelse(fl.reg$dem==1 | fl.reg$rep==1,1,0)


reg.count = fl.reg[year(as.Date(birthdate))>=1975 & race %in% 3:5, list(registered = .N,
                                                                                      democrat = sum(dem),
                                                                                      republican = sum(rep)), by = c("birthdate", "race")]

counts = merge(fl.count, reg.count, by = c("race", "birthdate"), all.x = T, all.y=T)

data=counts



save(data, file = "data/counts/counts_fl09.RData")